package com.carleasoft.mps.cms.business.repairs.service.Impl;

import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.repairs.bean.entity.*;
import com.carleasoft.mps.cms.business.repairs.bean.po.*;
import com.carleasoft.mps.cms.business.repairs.bean.vo.*;
import com.carleasoft.mps.cms.business.repairs.constants.ExceptionRepairsEnum;
import com.carleasoft.mps.cms.business.repairs.dao.RepairsGroupMapper;
import com.carleasoft.mps.cms.business.repairs.dao.VehicleMessageMapper;
import com.carleasoft.mps.cms.business.repairs.service.*;
import com.carleasoft.mps.cms.business.train.bean.po.DeptInfoVos;
import com.carleasoft.mps.cms.business.train.constants.ExceptionTrainEnum;
import com.carleasoft.mps.cms.business.train.dao.TrainExamMapper;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import com.carleasoft.mps.excel.utils.ConvertTemplatePath;
import com.carleasoft.mps.excel.utils.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 装备报修 服务实现类
 * </p>
 *
 * @author fan
 * @since 2023-06-02
 */
@Service
public class VehicleMessageServiceImpl extends ServiceImpl<VehicleMessageMapper, VehicleMessage> implements VehicleMessageService {

    @Autowired
    VehicleMaintainMessageService vehicleMaintainMessageService;
    @Autowired
    VehiclePersonService vehiclePersonService;
    @Autowired
    RepairsGroupMapper repairsGroupMapper;
    @Autowired
    TrainExamMapper trainExamMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean vehicleExcel(MultipartFile file) {
        boolean isOk = false;
        List<VehicleExcelVo> list = null;
        try {
            list = ExcelUtils.importExcel(file, VehicleExcelVo.class);
            list = list.stream().filter(i -> i.getEquipNo() != null).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (CollUtil.isNotEmpty(list)){
            Set<String> userNames = list.stream().map(VehicleExcelVo::getRepairsPersonName).collect(Collectors.toSet());
            List<UserInfoVo> userInfoVos = this.getBaseMapper().userInfo(userNames);
            Map<String, List<UserInfoVo>> map = userInfoVos.stream().collect(Collectors.groupingBy(UserInfoVo::getUserName));

            //单位信息
            Set<String> deptNames = list.stream().map(VehicleExcelVo::getDeptName).collect(Collectors.toSet());
            List<DeptInfoVos> deptInfoVos = trainExamMapper.deptInfo(deptNames);
            Map<String, DeptInfoVos> deptMap = deptInfoVos.stream().collect(Collectors.toMap(DeptInfoVos::getDeptName, Function.identity()));

            //装备鉴定修理工信息
            Set<String> equipNames = list.stream().map(VehicleExcelVo::getEquipAppraiseName).collect(Collectors.toSet());
            List<UserInfoVo> equipUserVos = this.getBaseMapper().userInfo(equipNames);
            Map<String, List<UserInfoVo>> equipMap = equipUserVos.stream().collect(Collectors.groupingBy(UserInfoVo::getUserName));

            for (VehicleExcelVo vehicleExcelVo : list) {
                DeptInfoVos infoVos = deptMap.get(vehicleExcelVo.getDeptName());
                VehicleMessageAddOrEditPo vehicleMessageAddOrEditPo = new VehicleMessageAddOrEditPo();
                vehicleMessageAddOrEditPo.setRepairsType(2);//默认装备报修
                vehicleMessageAddOrEditPo.setDeptCode(infoVos.getDeptCode());
                vehicleMessageAddOrEditPo.setDeptName(infoVos.getDeptName());
                vehicleMessageAddOrEditPo.setEquipType(vehicleExcelVo.getEquipType());
                vehicleMessageAddOrEditPo.setEquipModel(vehicleExcelVo.getEquipModel());
                vehicleMessageAddOrEditPo.setLicensePlateNumber(vehicleExcelVo.getLicensePlateNumber());
                vehicleMessageAddOrEditPo.setEquipNo(vehicleExcelVo.getEquipNo());
                vehicleMessageAddOrEditPo.setEngineNo(vehicleExcelVo.getEngineNo());
                vehicleMessageAddOrEditPo.setMileage(vehicleExcelVo.getMileage());
                vehicleMessageAddOrEditPo.setDescFault(vehicleExcelVo.getDescFault());
                vehicleMessageAddOrEditPo.setExternalRepairDemand(vehicleExcelVo.getExternalRepairDemand());
                if(ObjectUtil.isNotEmpty(equipMap)){
                    vehicleMessageAddOrEditPo.setEquipAppraiseNo(equipMap.get(vehicleExcelVo.getEquipAppraiseName()).get(0).getUserCode());
                }
                vehicleMessageAddOrEditPo.setEquipAppraiseName(vehicleExcelVo.getEquipAppraiseName());
                vehicleMessageAddOrEditPo.setPositionFaultTime(vehicleExcelVo.getPositionFaultTime());
                vehicleMessageAddOrEditPo.setExpectTime(vehicleExcelVo.getExpectTime());
                vehicleMessageAddOrEditPo.setPositionFault(vehicleExcelVo.getPositionFault());
                if(ObjectUtil.isNotEmpty(map)){
                    vehicleMessageAddOrEditPo.setRepairsPersonCode(map.get(vehicleExcelVo.getRepairsPersonName()).get(0).getUserCode());
                }
                vehicleMessageAddOrEditPo.setRepairsPersonName(vehicleExcelVo.getRepairsPersonName());
                vehicleMessageAddOrEditPo.setRepairsPersonTel(vehicleExcelVo.getRepairsPersonTel());
                isOk = this.vehicleAdd(vehicleMessageAddOrEditPo);
            }
        }

        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean vehicleAdd(VehicleMessageAddOrEditPo vehicleMessageAddOrEditPo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        VehicleMessage vehicleMessage = new VehicleMessage();
        BeanUtils.copyProperties(vehicleMessageAddOrEditPo, vehicleMessage);
        vehicleMessage.setBusinessCode(UUID.randomUUID().toString().replaceAll("-", ""));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
        String date1 = simpleDateFormat.format(new Date());
        String date2 = simpleDateFormat2.format(new Date());
        Integer theDayNum = this.baseMapper.getTheDayNum(date1);
        vehicleMessage.setRepairsNum(this.createNum(date2, theDayNum));
        vehicleMessage.setCreatePersonCode(user.getUserCode());
        vehicleMessage.setCreatePersonName(user.getUserName());
        vehicleMessage.setCreateTime(LocalDateTime.now());
        vehicleMessage.setUpdatePersonCode(user.getUserCode());
        vehicleMessage.setUpdatePersonName(user.getUserName());
        vehicleMessage.setUpdateTime(LocalDateTime.now());
        vehicleMessage.setStatus(1);
        return this.save(vehicleMessage);
    }

    @Override
    public Page<VehicleMessage> vehiclePage(VehicleMessagePagePo vehicleMessagePagePo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        vehicleMessagePagePo.setUserCode(user.getUserCode());
        Integer num1 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 1);
        Integer num2 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 2);
        Integer num3 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 3);
        List<Integer> typeList = new LinkedList<>();
        if (num1 > 0) {
            typeList.add(1);
        }
        if (num2 > 0) {
            typeList.add(2);
        }
        if (num3 > 0) {
            typeList.add(3);
        }
        vehicleMessagePagePo.setTypeList(typeList);
        List<VehicleMessage> list = this.list(Wrappers.lambdaQuery(VehicleMessage.class).eq(VehicleMessage::getMaintainPersonCode, user.getUserCode()));
        List<String> businessCodeList = new LinkedList<>();
        businessCodeList.add("-1");
        if (list != null && list.size() > 0) {
            list.stream().forEach(a -> {
                businessCodeList.add(a.getBusinessCode());
            });
        }
        vehicleMessagePagePo.setBusinessCodeList(businessCodeList);
        Page page = new Page(vehicleMessagePagePo.getCurrent(), vehicleMessagePagePo.getSize());
        return this.baseMapper.vehiclePage(page, vehicleMessagePagePo);
    }

    @Override
    public Page<VehicleMessage> myVehiclePage(VehicleMessagePagePo vehicleMessagePagePo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        vehicleMessagePagePo.setUserCode(user.getUserCode());
        Page page = new Page(vehicleMessagePagePo.getCurrent(), vehicleMessagePagePo.getSize());
        return this.baseMapper.myVehiclePage(page, vehicleMessagePagePo);
    }

    @Override
    public VehicleMessageDetailVo vehicleDetail(VehicleMessage vehicleMessage) {
        IBeanUtil.paramVerify(vehicleMessage, "businessCode");
        VehicleMessageDetailVo vehicleMessageDetailVo = new VehicleMessageDetailVo();
        VehicleMessage one = this.getOne(Wrappers.lambdaQuery(VehicleMessage.class).eq(VehicleMessage::getBusinessCode, vehicleMessage.getBusinessCode()));
        if (one != null) {
            vehicleMessageDetailVo.setVehicleMessage(one);
            vehicleMessageDetailVo.setVehicleMaintainMessage(vehicleMaintainMessageService.getOne(Wrappers.lambdaQuery(VehicleMaintainMessage.class).eq(VehicleMaintainMessage::getRepairsCode, one.getBusinessCode())));
        }
        return vehicleMessageDetailVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean vehicleDeal(VehicleMessageDealPo vehicleMessageDealPo) {
        IBeanUtil.paramVerify(vehicleMessageDealPo, "dealStatus", "businessCode");
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        Integer dealStatus = vehicleMessageDealPo.getDealStatus();// 处理状态(1-接单 2-派单 3-取消维修)
        LambdaUpdateWrapper<VehicleMessage> updateWrapper = Wrappers.lambdaUpdate(VehicleMessage.class);
        updateWrapper.eq(VehicleMessage::getBusinessCode, vehicleMessageDealPo.getBusinessCode());
        if (dealStatus == 1) {
            updateWrapper.set(VehicleMessage::getStatus, 2);//维修中
            updateWrapper.set(VehicleMessage::getMaintainPersonCode, user.getUserCode());
            updateWrapper.set(VehicleMessage::getMaintainPersonName, user.getUserName());
        }
        if (dealStatus == 2) {
            updateWrapper.set(VehicleMessage::getStatus, 3);//派工
            List<String> personCodeList = vehicleMessageDealPo.getPersonCodeList();
            if (personCodeList == null || personCodeList.size() <= 0) {
                throw new BusinessException(ExceptionRepairsEnum.DEAL_ERROR);
            }
            List<VehiclePerson> list = new LinkedList<>();
            personCodeList.stream().forEach(a -> {
                VehiclePerson vehiclePerson = new VehiclePerson();
                vehiclePerson.setRepairsCode(vehicleMessageDealPo.getBusinessCode());
                vehiclePerson.setMaintainPersonCode(a);
                list.add(vehiclePerson);
            });
            //设置派工的那个人为维修人
            VehiclePerson vehiclePerson = list.get(0);
            updateWrapper.set(VehicleMessage::getMaintainPersonCode, vehiclePerson.getMaintainPersonCode());
            updateWrapper.set(VehicleMessage::getMaintainPersonName, vehiclePersonService.getUserNameByUserCode(vehiclePerson.getMaintainPersonCode()));
            vehiclePersonService.remove(new QueryWrapper<VehiclePerson>().eq("REPAIRS_CODE", vehicleMessageDealPo.getBusinessCode()));//删除原来处理人
            vehiclePersonService.saveBatch(list);
        }
        if (dealStatus == 3) {
            updateWrapper.set(VehicleMessage::getStatus, 4);//未接单
        }
        return this.update(updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean vehicleFeedback(VehicleFeedbackPo vehicleFeedbackPo) {
        IBeanUtil.paramVerify(vehicleFeedbackPo, "businessCode");
        VehicleMessage vehicleMessageOne = this.getOne(new QueryWrapper<VehicleMessage>().eq("BUSINESS_CODE", vehicleFeedbackPo.getBusinessCode()));

        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        LambdaUpdateWrapper<VehicleMessage> updateWrapper = Wrappers.lambdaUpdate(VehicleMessage.class);
        updateWrapper.eq(VehicleMessage::getBusinessCode, vehicleFeedbackPo.getBusinessCode());
        updateWrapper.set(VehicleMessage::getMaintainPersonCode, user.getUserCode());
        updateWrapper.set(VehicleMessage::getMaintainPersonName, user.getUserName());
        updateWrapper.set(VehicleMessage::getStatus, 5);//已完成
        //保存维修信息
        VehicleMaintainMessage vehicleMaintainMessage = vehicleFeedbackPo.getVehicleMaintainMessage();
        vehicleMaintainMessage.setRepairsCode(vehicleFeedbackPo.getBusinessCode());
        vehicleMaintainMessage.setCreatePersonCode(user.getUserCode());
        vehicleMaintainMessage.setCreatePersonName(user.getUserName());
        vehicleMaintainMessage.setCreateTime(LocalDateTime.now());
        vehicleMaintainMessage.setUpdatePersonCode(user.getUserCode());
        vehicleMaintainMessage.setUpdatePersonName(user.getUserName());
        vehicleMaintainMessage.setUpdateTime(LocalDateTime.now());
        vehicleMaintainMessage.setBusinessCode(UUID.randomUUID().toString().replaceAll("-", ""));
        vehicleMaintainMessageService.save(vehicleMaintainMessage);
        RepairsGroup repairsGroup = repairsGroupMapper.selectOne(new QueryWrapper<RepairsGroup>().eq("PERSON_CODE", user.getUserCode()).like("PERSON_TYPE", vehicleMessageOne.getRepairsType()));
        if (repairsGroup != null) {
            if (vehicleMessageOne.getRepairsType() == 1) {
                repairsGroup.setVehicleMaintainNum(repairsGroup.getVehicleMaintainNum() + 1);
            }
            if (vehicleMessageOne.getRepairsType() == 2) {
                repairsGroup.setEquipMaintainNum(repairsGroup.getEquipMaintainNum() + 1);
            }
            if (vehicleMessageOne.getRepairsType() == 3) {
                repairsGroup.setBarracksMaintainNum(repairsGroup.getBarracksMaintainNum() + 1);
            }
            repairsGroupMapper.updateById(repairsGroup);
        }
        return this.update(updateWrapper);
    }

    @Override
    public List<RepairsGroup> sendPersonList(SendPersonListPo sendPersonListPo) {
        IBeanUtil.paramVerify(sendPersonListPo, "businessCode");
        List<String> list = new LinkedList<>();
        list.add("-1");
        //获取该报修已经指派过的人
        LambdaQueryWrapper<VehiclePerson> eq = Wrappers.lambdaQuery(VehiclePerson.class).eq(VehiclePerson::getRepairsCode, sendPersonListPo.getBusinessCode());
        List<VehiclePerson> list1 = vehiclePersonService.list(eq);
        if (list1 != null && list1.size() > 0) {
            list1.stream().forEach(a -> {
                list.add(a.getMaintainPersonCode());
            });
        }
        return repairsGroupMapper.sendPersonList(list, sendPersonListPo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean anew(AnewPo anewPo) {
        LambdaUpdateWrapper<VehicleMessage> updateWrapper = Wrappers.lambdaUpdate(VehicleMessage.class);
        updateWrapper.eq(VehicleMessage::getBusinessCode, anewPo.getBusinessCode());
        updateWrapper.set(VehicleMessage::getStatus, 3);//派工
        List<String> personCodeList = anewPo.getPersonCodeList();
        if (personCodeList == null || personCodeList.size() <= 0) {
            throw new BusinessException(ExceptionRepairsEnum.DEAL_ERROR);
        }
        List<VehiclePerson> list = new LinkedList<>();
        personCodeList.stream().forEach(a -> {
            VehiclePerson vehiclePerson = new VehiclePerson();
            vehiclePerson.setRepairsCode(anewPo.getBusinessCode());
            vehiclePerson.setMaintainPersonCode(a);
            list.add(vehiclePerson);
        });
        //设置派工的那个人为维修人
        VehiclePerson vehiclePerson = list.get(0);
        updateWrapper.set(VehicleMessage::getMaintainPersonCode, vehiclePerson.getMaintainPersonCode());
        updateWrapper.set(VehicleMessage::getMaintainPersonName, vehiclePersonService.getUserNameByUserCode(vehiclePerson.getMaintainPersonCode()));
        vehiclePersonService.remove(new QueryWrapper<VehiclePerson>().eq("REPAIRS_CODE", anewPo.getBusinessCode()));//删除原来处理人
        vehiclePersonService.saveBatch(list);
        return this.update(updateWrapper);
    }

    @Override
    public QueryIsDealPersonVo queryIsDealPerson() {
        QueryIsDealPersonVo queryIsDealPersonVo = new QueryIsDealPersonVo();
        queryIsDealPersonVo.setIsBarracks(0);
        queryIsDealPersonVo.setIsCar(0);
        queryIsDealPersonVo.setIsEquip(0);
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        Integer num1 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 1);
        Integer num2 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 2);
        Integer num3 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 3);
        if (num1 > 0) {
            queryIsDealPersonVo.setIsCar(1);
        }
        if (num2 > 0) {
            queryIsDealPersonVo.setIsEquip(1);
        }
        if (num3 > 0) {
            queryIsDealPersonVo.setIsBarracks(1);
        }
        return queryIsDealPersonVo;
    }

    @Override
    public boolean export(VehicleMessagePagePo vehicleMessagePagePo, HttpServletResponse response) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        vehicleMessagePagePo.setUserCode(user.getUserCode());
        Integer num1 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 1);
        Integer num2 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 2);
        Integer num3 = repairsGroupMapper.synthesizePerson(user.getUserCode(), 3);
        List<Integer> typeList = new LinkedList<>();
        if (num1 > 0) {
            typeList.add(1);
        }
        if (num2 > 0) {
            typeList.add(2);
        }
        if (num3 > 0) {
            typeList.add(3);
        }
        vehicleMessagePagePo.setTypeList(typeList);
        List<VehiclePerson> list = vehiclePersonService.list(Wrappers.lambdaQuery(VehiclePerson.class).eq(VehiclePerson::getMaintainPersonCode, user.getUserCode()));
        List<String> businessCodeList = new LinkedList<>();
        businessCodeList.add("-1");
        if (list != null && list.size() > 0) {
            list.stream().forEach(a -> {
                businessCodeList.add(a.getRepairsCode());
            });
        }
        vehicleMessagePagePo.setBusinessCodeList(businessCodeList);
        List<VehicleMessage> vehicleMessages = this.baseMapper.vehiclePage1(vehicleMessagePagePo);
        List<RepairsMessageVo> repairsMessageVos = new LinkedList<>();
        if (vehicleMessages != null && vehicleMessages.size() > 0) {
            vehicleMessages.stream().forEach(a -> {
                RepairsMessageVo repairsMessageVo = new RepairsMessageVo();
                BeanUtils.copyProperties(a, repairsMessageVo);
                if (a.getStatus() == 1) {
                    repairsMessageVo.setStatus("待处理");
                }
                if (a.getStatus() == 2) {
                    repairsMessageVo.setStatus("维修中");
                }
                if (a.getStatus() == 3) {
                    repairsMessageVo.setStatus("已派工");
                }
                if (a.getStatus() == 4) {
                    repairsMessageVo.setStatus("未接单");
                }
                if (a.getStatus() == 5) {
                    repairsMessageVo.setStatus("已完成");
                }
                if (a.getRepairsType() == 1) {
                    repairsMessageVo.setRepairsType("器材申请");
                }
                if (a.getRepairsType() == 2) {
                    repairsMessageVo.setRepairsType("装备报修");
                }
                if (a.getRepairsType() == 3) {
                    repairsMessageVo.setRepairsType("营房报修");
                }
                repairsMessageVos.add(repairsMessageVo);
            });
        }
        Map<String, Object> map = new HashMap<>();
        String path = "\\templates\\vehicleExcel.xls";
        TemplateExportParams templatePath = new TemplateExportParams(ConvertTemplatePath.convertTemplatePath(path));
        map.put("list", repairsMessageVos);
        try {
            ExcelUtils.exportExcel(templatePath, map, "导入报修记录", response);
        } catch (IOException e) {
            throw new BusinessException(ExceptionTrainEnum.TEMP_ERROR);
        }
        return true;
    }

    @Override
    public VehicleStatisticsVo statistics() {
        VehicleStatisticsVo vehicleStatisticsVo = new VehicleStatisticsVo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String theMonth = simpleDateFormat.format(new Date());
        vehicleStatisticsVo.setTheMonthCar(this.baseMapper.statisticsRepair(theMonth, 1));
        vehicleStatisticsVo.setTheMonthEquip(this.baseMapper.statisticsRepair(theMonth, 2));
        vehicleStatisticsVo.setTheMonthBarracks(this.baseMapper.statisticsRepair(theMonth, 3));
        vehicleStatisticsVo.setAllCar(this.baseMapper.statisticsRepair(null, 1));
        vehicleStatisticsVo.setAllEquip(this.baseMapper.statisticsRepair(null, 2));
        vehicleStatisticsVo.setAllBarracks(this.baseMapper.statisticsRepair(null, 3));
        return vehicleStatisticsVo;
    }

    @Override
    public List<HashMap<String, Object>> statisticsTheYear() {
        List<HashMap<String, Object>> returnMapList = new LinkedList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String theYear = simpleDateFormat.format(new Date());
        HashMap<String, Object> hashMap1 = new HashMap<>();
        hashMap1.put("month", 1);
        hashMap1.put("car", this.baseMapper.statisticsByMonth(theYear + "-01", 1));
        hashMap1.put("equip", this.baseMapper.statisticsByMonth(theYear + "-01", 2));
        hashMap1.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-01", 3));
        returnMapList.add(hashMap1);

        HashMap<String, Object> hashMap2 = new HashMap<>();
        hashMap2.put("month", 2);
        hashMap2.put("car", this.baseMapper.statisticsByMonth(theYear + "-02", 1));
        hashMap2.put("equip", this.baseMapper.statisticsByMonth(theYear + "-02", 2));
        hashMap2.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-02", 3));
        returnMapList.add(hashMap2);

        HashMap<String, Object> hashMap3 = new HashMap<>();
        hashMap3.put("month", 3);
        hashMap3.put("car", this.baseMapper.statisticsByMonth(theYear + "-03", 1));
        hashMap3.put("equip", this.baseMapper.statisticsByMonth(theYear + "-03", 2));
        hashMap3.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-03", 3));
        returnMapList.add(hashMap3);

        HashMap<String, Object> hashMap4 = new HashMap<>();
        hashMap4.put("month", 4);
        hashMap4.put("car", this.baseMapper.statisticsByMonth(theYear + "-04", 1));
        hashMap4.put("equip", this.baseMapper.statisticsByMonth(theYear + "-04", 2));
        hashMap4.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-04", 3));
        returnMapList.add(hashMap4);

        HashMap<String, Object> hashMap5 = new HashMap<>();
        hashMap5.put("month", 5);
        hashMap5.put("car", this.baseMapper.statisticsByMonth(theYear + "-05", 1));
        hashMap5.put("equip", this.baseMapper.statisticsByMonth(theYear + "-05", 2));
        hashMap5.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-05", 3));
        returnMapList.add(hashMap5);

        HashMap<String, Object> hashMap6 = new HashMap<>();
        hashMap6.put("month", 6);
        hashMap6.put("car", this.baseMapper.statisticsByMonth(theYear + "-06", 1));
        hashMap6.put("equip", this.baseMapper.statisticsByMonth(theYear + "-06", 2));
        hashMap6.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-06", 3));
        returnMapList.add(hashMap6);

        HashMap<String, Object> hashMap7 = new HashMap<>();
        hashMap7.put("month", 7);
        hashMap7.put("car", this.baseMapper.statisticsByMonth(theYear + "-07", 1));
        hashMap7.put("equip", this.baseMapper.statisticsByMonth(theYear + "-07", 2));
        hashMap7.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-07", 3));
        returnMapList.add(hashMap7);

        HashMap<String, Object> hashMap8 = new HashMap<>();
        hashMap8.put("month", 8);
        hashMap8.put("car", this.baseMapper.statisticsByMonth(theYear + "-08", 1));
        hashMap8.put("equip", this.baseMapper.statisticsByMonth(theYear + "-08", 2));
        hashMap8.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-08", 3));
        returnMapList.add(hashMap8);

        HashMap<String, Object> hashMap9 = new HashMap<>();
        hashMap9.put("month", 9);
        hashMap9.put("car", this.baseMapper.statisticsByMonth(theYear + "-09", 1));
        hashMap9.put("equip", this.baseMapper.statisticsByMonth(theYear + "-09", 2));
        hashMap9.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-09", 3));
        returnMapList.add(hashMap9);

        HashMap<String, Object> hashMap10 = new HashMap<>();
        hashMap10.put("month", 10);
        hashMap10.put("car", this.baseMapper.statisticsByMonth(theYear + "-10", 1));
        hashMap10.put("equip", this.baseMapper.statisticsByMonth(theYear + "-10", 2));
        hashMap10.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-10", 3));
        returnMapList.add(hashMap10);

        HashMap<String, Object> hashMap11 = new HashMap<>();
        hashMap11.put("month", 11);
        hashMap11.put("car", this.baseMapper.statisticsByMonth(theYear + "-11", 1));
        hashMap11.put("equip", this.baseMapper.statisticsByMonth(theYear + "-11", 2));
        hashMap11.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-11", 3));
        returnMapList.add(hashMap11);

        HashMap<String, Object> hashMap12 = new HashMap<>();
        hashMap12.put("month", 12);
        hashMap12.put("car", this.baseMapper.statisticsByMonth(theYear + "-12", 1));
        hashMap12.put("equip", this.baseMapper.statisticsByMonth(theYear + "-12", 2));
        hashMap12.put("barracks", this.baseMapper.statisticsByMonth(theYear + "-12", 3));
        returnMapList.add(hashMap12);
        return returnMapList;
    }

    @Override
    public MaintainRateVo MaintainRate() {
        MaintainRateVo maintainRateVo = new MaintainRateVo();
        Integer carAll = this.baseMapper.mainTainNum(1, null);//所有车辆报修
        maintainRateVo.setCarAll(carAll);
        Integer carDeal = this.baseMapper.mainTainNum(1, 5);//完成车辆报修
        maintainRateVo.setCarDeal(carDeal);
        Integer eqAll = this.baseMapper.mainTainNum(2, null);//所有装备报修
        maintainRateVo.setEqAll(eqAll);
        Integer eqDeal = this.baseMapper.mainTainNum(2, 5);//完成装备报修
        maintainRateVo.setEqDeal(eqDeal);
        Integer barracksAll = this.baseMapper.mainTainNum(2, null);//所有营房报修
        maintainRateVo.setBarracksAll(barracksAll);
        Integer barracksDeal = this.baseMapper.mainTainNum(2, 5);//完成营房报修
        maintainRateVo.setBarracksDeal(barracksDeal);
        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(2);
        if (carAll > 0) {
            maintainRateVo.setCarRate(numberFormat.format(carDeal / (carAll * 1.00)));
        } else {
            maintainRateVo.setCarRate("0.00%");
        }
        if (eqAll > 0) {
            maintainRateVo.setEquipRate(numberFormat.format(eqDeal / (eqAll * 1.00)));
        } else {
            maintainRateVo.setEquipRate("0.00%");
        }
        if (barracksAll > 0) {
            maintainRateVo.setBarracksRate(numberFormat.format(barracksDeal / (barracksAll * 1.00)));
        } else {
            maintainRateVo.setBarracksRate("0.00%");
        }
        return maintainRateVo;
    }


    String createNum(String date, Integer num) {
        String r = date;
        if (num < 10) {
            r = r + "00" + num;
        }
        if (num >= 10 && num < 100) {
            r = r + "0" + num;
        }
        if (num >= 100) {
            r = r + num + "";
        }
        return r;
    }
}
